﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace EngineOfEvermore.Editor.Common
{
    public class UndoRecord : IDisposable
    {
        #region UndoEntry
        class UndoEntry
        {
            public UndoAction UndoAction;
            public int Version;
        }
        #endregion


        #region Variables
        bool _disposed = false;
        List<UndoEntry> _undoActionList = new List<UndoEntry>();
        int _actionIndex = -1;
        bool _ignoreNextAction = false;
        int _maxVersion = -1;
        #endregion

        #region Finalizer
        ~UndoRecord()
        {
            Dispose( false );
        }
        #endregion

        #region Properties
        public bool CanUndo
        {
            get 
            {
                return _undoActionList.Any() && _actionIndex >= 0; 
            }
        }

        public bool CanRedo
        {
            get 
            {
                bool isLastAction = ( _actionIndex == _undoActionList.Count - 1 );
                return !isLastAction && _undoActionList.Any(); 
            }
        }

        public int Version
        {
            get 
            {
                if ( _actionIndex >= 0 && _actionIndex < _undoActionList.Count )
                {
                    return _undoActionList[_actionIndex].Version;
                }
                else
                {
                    return -1;
                }
            }
        }
        #endregion

        #region Methods
        public void Undo()
        {
            if ( !CanUndo )
                return;

            Debug.Assert( _actionIndex >= 0 );
            Debug.Assert( _actionIndex < _undoActionList.Count );

            // Take one action from the list
            UndoAction action = _undoActionList[_actionIndex--].UndoAction;

            _ignoreNextAction = action.IgnoreNextAction;
            action.Undo();
        }

        public void Redo()
        {
            if ( !CanRedo )
                return;

            Debug.Assert( _actionIndex >= -1 );
            Debug.Assert( _actionIndex < _undoActionList.Count - 1);

            // Take next action in the list
            UndoAction action = _undoActionList[++_actionIndex].UndoAction;

            _ignoreNextAction = action.IgnoreNextAction;
            action.Redo();
        }

        public void AddUndoAction( UndoAction undoAction )
        {
            Debug.Assert( undoAction != null );

            if ( _ignoreNextAction )
            {
                // Discard the action
                IDisposable disposable = ( undoAction as IDisposable );
                if ( disposable != null )
                {
                    disposable.Dispose();
                }

                _ignoreNextAction = false;
            }
            else
            {
                // If the _actionIndex is not at the end of the
                // list remove anything after it
                Truncate();

                UndoEntry entry = new UndoEntry();
                entry.UndoAction = undoAction;
                entry.Version = ++_maxVersion;

                _undoActionList.Add( entry );
                ++_actionIndex;
            }
        }

        public void Truncate()
        {
            if ( _actionIndex != _undoActionList.Count - 1 )
            {
                for ( int i = _actionIndex + 1; i < _undoActionList.Count; ++i )
                {
                    IDisposable disposable = 
                        ( _undoActionList[i].UndoAction as IDisposable );

                    if ( disposable != null )
                    {
                        disposable.Dispose();
                    }
                }

                _undoActionList.RemoveRange(
                    _actionIndex + 1,
                    _undoActionList.Count - ( _actionIndex + 1 )
                    );
            }
        }
        #endregion

        #region IDisposable Members

        protected void Dispose( bool disposing )
        {
            if ( !_disposed )
            {
                if ( disposing )
                {
                    foreach ( UndoAction action in 
                        _undoActionList.Select( x => x.UndoAction ) )
                    {
                        IDisposable disposable = action as IDisposable;

                        if ( disposable != null )
                        {
                            disposable.Dispose();
                        }
                    }

                    _undoActionList.Clear();
                    _undoActionList = null;
                }

                _disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose( true );

            GC.SuppressFinalize( this );
        }

        #endregion
    }
}
